package cn.org.july.weixin.service.event.listener;

import cn.org.july.weixin.common.bean.message.TextMessage;
import cn.org.july.weixin.common.constant.EventType;
import cn.org.july.weixin.common.utils.HttpClientUtils;
import cn.org.july.weixin.service.config.WxConfig;
import cn.org.july.weixin.service.event.Event;
import cn.org.july.weixin.service.event.EventListener;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

/**
 * 关注公共号执行事件
 * Created with IntelliJ IDEA.
 * User:  wanghongjie
 * Date:  2020/9/1 - 23:18
 * <p>
 * Description:
 *
 * @blame Android Team
 */
@Service
public class SubscribeListener implements EventListener {

    @Resource
    private WxConfig config;

    @Override
    public String handleEvent(Event event) {
        String url = config.getHttpUrl().concat("message/").concat(EventType.SUBSCRIBE.name())
                .concat("/").concat(EventType.SUBSCRIBE.name());
        String content = "欢迎关注公众号,连续签到可获取精美礼品喔...";
        try {
            String s = HttpClientUtils.get(url);
            JSONObject jsonObject = JSONObject.parseObject(s);
            content = jsonObject.getString("data");
        } catch (IOException e) {
        }
        TextMessage of = TextMessage.ofSendMsg(event.getMessage(), content);
        return of.toXml();
    }

    @Override
    public EventType getEvent() {
        return EventType.SUBSCRIBE;
    }
}